<template>
  <div>
    <el-form :model="form" inline v-if="WarehouseTypeList">

      <el-form-item label="货区类型:" width="85">
        <el-select v-model="form.id" style="width: 120px;" clearable>
          <el-option :label="item.name" :value="item.id" v-for="item in WarehouseTypeList" :key="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="货区编号:">
        <el-input v-model="form.code" clearable style="width: 120px;" />
      </el-form-item>
      <el-form-item label="仓库:">
        <el-select v-model="form.warehouseId" style="width: 120px;" clearable>
          <el-option :label="item.name" :value="item.id" v-for="item in NewHouseList" :key="item.id"
            @click='getWarehouseTypeList(item.id)' />
        </el-select>
      </el-form-item>
      <el-form-item label="是否停用:">
        <el-select v-model="form.isStop" style="width: 120px;" clearable>
          <el-option label="全部" :value="-1" />
          <el-option label="否" :value="0" />
          <el-option label="是" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间:">
        <el-date-picker v-model="times" type="datetimerange" start-placeholder="开始时间" value-format="x" width="125"
          end-placeholder="结束时间" time-format="A hh:mm:ss" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
        <el-button type="primary" @click="createNewGoods">新建货区</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="table">
    <el-table ref="multipleTableRef" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange"
      border>
      <el-table-column type="selection" width="55" />
      <el-table-column label="仓库名称" width="180">
        <template #default="scope">{{ scope.row.warehouseName }}</template>
      </el-table-column>
      <el-table-column label="货区类型" width="180">
        <template #default="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="是否停用" width="180">
        <template #default="scope">{{ scope.row.isStop == 0 ? '否' : '是' }}</template>
      </el-table-column>
      <el-table-column label="创建时间" width="180">
        <template #default="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button link type="primary" size="small" @click.prevent="setType(scope.row)">
            编辑
          </el-button>
          <el-button link type="primary" size="small" @click.prevent="deleteType(scope.row)">
            删除
          </el-button>
          <el-button link type="primary" size="small" @click.prevent="$router.push('/home/everyView/warehouseSeat')">
            查看库位
          </el-button>
          <el-button link type="primary" size="small" @click.prevent="printShop(scope.row)">
            打印货区条码
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="page">
    <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
  <div>
    <el-dialog v-model="CreateDialogVisible" width="250">
      <span>
        <el-form :model="Createform" inline>

          <el-form-item label="所在仓库:" width="85">
            <el-select v-model="Createform.warehouseId" style="width: 120px;">
              <el-option :label="item.name" :value="item.id" v-for="item in houseList" :key="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="货区类型:">
            <!-- <el-select v-model="Createform.name" style="width: 120px;"> -->
            <el-input v-model="Createform.name" clearable style="width: 120px;" />
            <!-- <el-option :label="item.name" :value="item.id" v-for="item in NewWarehouseTypeList" :key="item.id" /> -->
            <!-- </el-select> -->
          </el-form-item>
          <el-form-item label="是否停用：">
            <div>
              <label>
                <input type="radio" value="0" v-model="Createform.isStop"> 否
              </label>
              <label>
                <input type="radio" value="1" v-model="Createform.isStop"> 是
              </label>
            </div>
          </el-form-item>
        </el-form>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="sbmintCereate">
            保存
          </el-button>
          <el-button @click="CreateDialogVisible = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onBeforeMount, onMounted, onBeforeUnmount } from 'vue'
import { getWarehouseDownList, getWarehouseAreaDownList, warehouseAreaPrintCode, warehouseAreaCreate, warehouseArealist, warehouseAreaupdate, warehouseAreadelete } from '@/api/index'
// do not use same name with ref
import { ElMessage } from 'element-plus'
import { useAlertsStore } from '@/store/index'
const store = useAlertsStore()
const form = ref({
  id: -1,
  code: '',
  warehouseId: -1,
  isStop: -1,
  beginDateTime: '',
  endDateTime: '',
  pageSize: 10,
  pageNum: 1
})
const times = ref()
const tableData = ref([])
onBeforeMount(() => {
  wareHouse()
  getWarehouseTypeList(-1)
  getList()
})
const onSubmit = () => {
  console.log('submit!')
  getList()
}
const getList = () => {
  if (times.value) {
    console.log(times)
    form.value.beginDateTime = times.value[0]
    form.value.endDateTime = times.value[1]
  } else {
    delete form.value.beginDateTime
    delete form.value.endDateTime
  }
  warehouseArealist(form.value).then(res => {
    const { data: { data } } = res
    total.value = data.total
    tableData.value = data.list
  })
}
// 分页
const currentPage4 = ref(1)
const pageSize4 = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  form.value.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  form.value.pageNum = val
  getList()
}

// table
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

// 仓库下拉
const houseList = ref([])
const NewHouseList = ref([])
const wareHouse = async () => {
  const house = await getWarehouseDownList()
  const { data: { data } } = house
  houseList.value = data
  NewHouseList.value = [...data]
  houseList.value.shift()
  console.log(houseList.value)
}
// 货区类型
const WarehouseTypeList = ref([])
const NewWarehouseTypeList = ref([])
const getWarehouseTypeList = (val) => {
  getWarehouseAreaDownList(val).then(res => {
    const { data: { data } } = res
    console.log('data')
    console.log(data)
    WarehouseTypeList.value = [...data]
    NewWarehouseTypeList.value = data
    NewWarehouseTypeList.value.shift()
  })
}
// 新建货区
const Createform = ref({})
const or = ref('')
const CreateDialogVisible = ref(false)
const createNewGoods = () => {
  or.value = '添加'
  Createform.value = {}
  CreateDialogVisible.value = true
}
const sbmintCereate = () => {
  if (or.value === '添加') {
    if (Createform.value.id) {
      delete Createform.value.id
    }
    warehouseAreaCreate(Createform.value).then(res => {
      getList()
      CreateDialogVisible.value = false
      ElMessage.success('操作成功')
    })
  } else if (or.value === '编辑') {
    warehouseAreaupdate(Createform.value).then(res => {
      getList()
      CreateDialogVisible.value = false
      ElMessage.success('操作成功')
    })
  }
}
// 编辑
const setType = (row) => {
  or.value = '编辑'
  Createform.value = {}
  Createform.value = row
  Createform.value.warehouseId = Number(Createform.value.warehouseId)
  CreateDialogVisible.value = true
}
// 删除
const deleteType = (row) => {
  warehouseAreadelete({ id: row.id }).then(res => {
    ElMessage.success('操作成功')
    getList()
  })
}
const printShop = (row) => {
  warehouseAreaPrintCode({ id: row.id }).then(res => {
    const { data: { data } } = res
    if (localStorage.getItem('PrinterSetListKW')) {
      const lists = JSON.parse(localStorage.getItem('PrinterSetListKW'))
      const val = { cmd: 'print', requestID: '123458976', version: '1.0', task: { taskID: (new Date().getTime() + ''), preview: false, printer: lists[0].PrinterList, notifyMode: 'allInOne', previewType: 'pdf', documents: [{ documentID: '0123456789', contents: [{ data: { width: '45', height: '35', padding: '2', barcodeHeight: '13', barcodeMargin: '1.8', fontSize: '8', showOrderName: '0', left: '2', waybillCode: data }, templateURL: 'http://yitaoweb.sc.kuaidi600.com/moban/express.xml' }] }] } }
      store.socket.send(JSON.stringify(val))
    } else {
      ElMessage.success('先设置打印机')
    }
  })
}
onMounted(() => {
  doConnect()
})
const doConnect = () => {
  console.log('d打开了不')
  store.socket = new WebSocket('ws://localhost:13528')
  // 打开Socket
  store.socket.onopen = function (event) {
    // 监听消息
    store.socket.onmessage = function (event) {
      // localStorage.setItem('printerproperties', event.data)
    }
    // 监听Socket的关闭
    store.socket.onclose = function (event) {
      // console.log('Client notified socket has closed', event)
    }
  }
}
// 销毁
onBeforeUnmount(() => {
  console.log('销毁')
  store.destroyed()
})
</script>

<style lang='less' scoped></style>
